import { ramdomByFloat } from "./ramdomByFloat";

/**
 * 仿真随机带曲线滑动
 * @param qx 起点x轴坐标
 * @param qy 起点y轴坐标
 * @param zx 终点x轴坐标
 * @param zy 终点y轴坐标
 * @param time 滑动时间，毫秒
 */
export function smlMove(qx: number, qy: number, zx: number, zy: number, time: number = 800) {
    const delay = time || ramdomByFloat(800)
    const xxy: [number, number][] = [];
    const dx0 = {
        "x": qx,
        "y": qy
    };
    const dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    const dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    const dx3 = {
        "x": zx,
        "y": zy
    };
    const point: any[] = [
        dx0,
        dx1,
        dx2,
        dx3
    ];
    const point1 = bezierCurves(point, 0.08)
    const point2 = bezierCurves(point, 0.16)
    for (let i = 0.16; i < 1; i += 0.08) {
        xxy.push(bezierCurves(point, i));
    }
    gesture(delay, point1, point2, ...xxy);
};


function bezierCurves(cp: any[], t: number): [number, number] {
    const cx = 3.0 * (cp[1].x - cp[0].x);
    const bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    const ax = cp[3].x - cp[0].x - cx - bx;
    const cy = 3.0 * (cp[1].y - cp[0].y);
    const by = 3.0 * (cp[2].y - cp[1].y) - cy;
    const ay = cp[3].y - cp[0].y - cy - by;
    let tSquared = t * t;
    const tCubed = tSquared * t;
    const result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return [result.x, result.y];
};
